package com.example.administrator.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ShareActionProvider;

import com.example.administrator.myapplication.Util.MyHelper;
import com.example.administrator.myapplication.Util.SpSave;

public class MainActivity extends AppCompatActivity {

    private EditText username;
    private EditText student;
    private Button loginBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initView() {
        username = findViewById(R.id.username_et);
        student = findViewById(R.id.student_id);
        loginBtn = findViewById(R.id.login_btn);

    }

    private void initData() {
        String usernameStr = username.getText().toString() + "";
        String studentStr = student.getText().toString() + "";
//        SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
//        //开启编辑器
//        SharedPreferences.Editor editor = sharedPreferences.edit();
//        //向编辑器里面存入数据
//        editor.putString("username", usernameStr);
//        editor.putString("student", studentStr);
//        //提交数据
//        editor.commit();
        SpSave.saveString(MainActivity.this, "username", usernameStr);
        SpSave.saveString(MainActivity.this, "student", studentStr);
        MyHelper myHelper = new MyHelper(MainActivity.this);
        myHelper.insert(MainActivity.this,usernameStr,studentStr);


    }

    private void initEvent() {
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                Intent intent = new Intent(MainActivity.this, TwoActivity.class);
                startActivity(intent);

            }
        });

    }
}
